package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.phone.app.AppApplication;

import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 状态数据列表
 */
public final class DeviceModeValueAdapter extends AppAdapter<DeviceModeValue> {

    private int currentSelectedPosition = -1;
    private boolean isHot = false;

    public DeviceModeValueAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    @Override
    public void setData(@Nullable List<DeviceModeValue> data) {
        currentSelectedPosition = -1;
        super.setData(data);
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private ViewHolder() {
            super(R.layout.item_mode_value);
        }

        @Override
        public void onBindView(int position) {
            DeviceModeValue item = getItem(position);
            TextView textView = (TextView) itemView;
            if(isHot){
                textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.mode_drawable_hot, 0, 0);
                textView.setTextColor(AppApplication.getInstance().getThemeColorStateList(R.attr.modeHotTextSelector));
            }else{
                textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, R.drawable.mode_drawable_clod, 0, 0);
                textView.setTextColor(AppApplication.getInstance().getThemeColorStateList(R.attr.modeColdTextSelector));
            }

            if(position == currentSelectedPosition){
                textView.setSelected(true);
            }else{
                textView.setSelected(false);
            }
            int nameRes = getResources().getIdentifier(item.getName(), "string", getContext().getPackageName());
            if(nameRes == 0){
                textView.setText(item.getName());
            }else{
                textView.setText(nameRes);
            }
            int selectedRes = AppApplication.getInstance().getResourceId(item.getImage(), "attr");
            int unSelectedRes = AppApplication.getInstance().getResourceId(item.getImage() + "_close", "attr");
            Drawable drawable = textView.getCompoundDrawables()[1];
            if (drawable instanceof StateListDrawable) {
                StateListDrawable stateListDrawable = (StateListDrawable) drawable;
                Drawable current = stateListDrawable.getCurrent();
                if (current instanceof LayerDrawable) {
                    LayerDrawable layerDrawable = (LayerDrawable) current;
                    if(textView.isSelected()){
                        layerDrawable.setDrawableByLayerId(R.id.selected_layer, AppApplication.getInstance().getThemeDrawable(selectedRes));
                    }else{
                        layerDrawable.setDrawableByLayerId(R.id.unselected_layer, AppApplication.getInstance().getThemeDrawable(unSelectedRes));
                    }
                }
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    public void selectItem(boolean isMode, int position) {
        currentSelectedPosition = position;
        if(isMode && getData() != null && currentSelectedPosition < getData().size()){
            DeviceModeValue deviceModeValue = getData().get(currentSelectedPosition);
            int nameRes = getResources().getIdentifier(deviceModeValue.getName(), "string", getContext().getPackageName());
            isHot = R.string.Air_Mode_Hot == nameRes;
        }
        notifyDataSetChanged();
    }

    public void selectItem(int position) {
        currentSelectedPosition = position;
        notifyDataSetChanged();
    }

    public void clearSelected() {
        currentSelectedPosition = -1;
        notifyDataSetChanged();
    }
}